package com.ruoyi.message.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.message.domain.SysMessage;
import com.ruoyi.message.service.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/message/sysMessage")
public class SysMessageController extends BaseController {

    @Autowired
    private ISysMessageService sysMessageService;

    /**
     * 查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMessage sysMessage) {
        startPage(); // 启用分页
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        return getDataTable(list); // 返回分页数据
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable Long messageId) {
        return AjaxResult.success(sysMessageService.selectSysMessageById(messageId));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:add')")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMessage sysMessage) {
        return toAjax(sysMessageService.insertSysMessage(sysMessage));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:edit')")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMessage sysMessage) {
        return toAjax(sysMessageService.updateSysMessage(sysMessage));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:remove')")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds) {
        return toAjax(sysMessageService.deleteSysMessageByIds(messageIds));
    }

    /**
     * 标记消息为已读
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:read')")
    @PutMapping("/read/{messageId}")
    public AjaxResult read(@PathVariable Long messageId) {
        sysMessageService.readSysMessage(messageId);
        return AjaxResult.success();
    }

    /**
     * 获取订单通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:orderDetail')")
    @GetMapping(value = "/orderDetail/{messageId}")
    public AjaxResult getOrderDetail(@PathVariable Long messageId) {
        SysMessage message = sysMessageService.selectSysMessageById(messageId);
        if (message == null) {
            return AjaxResult.error("消息不存在");
        }
        return AjaxResult.success(message);
    }

    /**
     * 同意阿姨
     */
    @PreAuthorize("@ss.hasPermi('message:sysMessage:agree')")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/agreeAunt")
    public AjaxResult agreeAunt(@RequestBody Map<String, Long> params) {
        Long messageId = params.get("messageId");
        if (messageId == null) {
            return AjaxResult.error("messageId不能为空");
        }

        SysMessage message = sysMessageService.selectSysMessageById(messageId);
        if (message == null) {
            return AjaxResult.error("消息不存在");
        }

        // 标记为已同意（例如修改状态为已读或增加标志字段）
        message.setStatus(1); // 已读状态
        sysMessageService.updateSysMessage(message);

        return AjaxResult.success(Map.of("agreed", true));
    }

}
